const Koa = require("koa");
const app = new Koa();

// Logger 中间件, 用于记录每个请求的详细信息
const logger = async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
};

app.use(logger);

// 中间件1
app.use(async (ctx, next) => {
  console.log("中间件1: 进入");
  await next();
  console.log("中间件1: 离开");
});

// 中间件2
app.use(async (ctx, next) => {
  console.log("中间件2: 进入");
  await next();
  console.log("中间件2: 离开");
});

// 响应中间件
app.use(async (ctx) => {
  console.log("响应中间件");
  ctx.body = "Hello Koa";
});

app.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});
